package org.theSeed.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
import org.theSeed.webFlux.filter.RepeatReadFilter;
import org.theSeed.webFlux.filter.ReqAndResLogFilter;
import org.theSeed.webFlux.filter.TraceFilter;
import org.theSeed.webFlux.handler.WebSocketHandlerImpl;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class WebConfig {
    @Bean
    public TraceFilter traceFilter(){
        return new TraceFilter(Integer.MIN_VALUE);
    }

    @Bean
    public RepeatReadFilter repeatReadFilter(){
        return new RepeatReadFilter(Integer.MIN_VALUE + 1);
    }

    @Bean
    public ReqAndResLogFilter reqAndResLogFilter(){
        return new ReqAndResLogFilter(Integer.MIN_VALUE + 10);
    }


    @Bean
    public HandlerMapping handlerMapping() {
        Map<String, WebSocketHandler> map = new HashMap<>();
        map.put("/ws/link", new WebSocketHandlerImpl());

        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setUrlMap(map);
        mapping.setOrder(-1); // 高优先级
        return mapping;
    }

    @Bean
    public WebSocketHandlerAdapter handlerAdapter() {
        return new WebSocketHandlerAdapter();
    }
}
